package com.example.algorithm.stacklist;

import java.util.HashMap;
import java.util.Stack;

/**
 * @author : zhy
 * @since : 2022/7/27
 * 有效括号
 */
public class Brackets {

    public boolean isValid(String s) {
        HashMap<String, String> map = new HashMap<>();
        map.put("(",")");
        map.put("{","}");
        map.put("[","]");
        String[] split = s.split("");
        Stack<String> stack=new Stack<>();
        int len=split.length;
        if(len%2!=0){
            return false;
        }
        for(int i=0;i<len;i++)
        {
            if(map.containsKey(split[i]))
            {
                stack.push(map.get(split[i]));
            }else {
                if(stack.isEmpty()){
                    return false;
                }
                String pop = stack.pop();
                if(!split[i].equals(pop))
                {
                   return false;
                }
            }
        }
        return stack.isEmpty()?true:false;
    }

    public static void main(String[] args) {
        Brackets brackets = new Brackets();
        System.out.println(brackets.isValid(")["));
    }
}
